<?php

namespace App\Listeners;

use App\Events\AllocateEvent;
use App\Events\LoanBuyEvent;
use App\Events\LoanTagEvent;
use App\Events\UpdateEvent;
use App\Models\LoanApplicationLog;
use App\Models\LoanTag;


class LoanEventSubscriber
{
    /**
     * 分派事件
     *
     * @param AllocateEvent $event
     * @return void
     */
    public function  handleAllocate(AllocateEvent $event) {

        $logModel = new LoanApplicationLog();
        $logModel->user_id = $event->fromUser->id;
        $logModel->loan_id = $event->loan->id;
        $logModel->org_root = $event->fromUser->org_root;
        $logModel->org_code = $event->fromUser->org_code;

        if ($event->toUser) {
            $logModel->comment = sprintf("指派给: %s", $event->toUser->name);
        } else if($event->toOrg) {
            $logModel->comment = sprintf("指派给: %s", $event->toOrg->name);
        }

        $logModel->save();
    }

    /**
     * 状态变更
     * @param UpdateEvent $event
     * @return void
     */
    public function  handleUpdate(UpdateEvent $event) {
        $logModel = new LoanApplicationLog();
        $logModel->user_id = $event->user->id;
        $logModel->loan_id = $event->originLoan['id'];
        $logModel->org_root = $event->user->org_root;
        $logModel->org_code = $event->user->org_code;

        $comment = [];
        if ($event->originLoan['status'] != $event->newLoan['status']) {
            $comment[]= sprintf("变更状态为: %s => %s", $event->originLoan['status_name'], $event->newLoan['status_name']);
        }

        if(!empty($comment)) {
            $logModel->comment = implode(',', $comment);
            $logModel->save();
        }

    }

    /**
     * @param LoanBuyEvent $event
     * @return void
     */
    public function handleBuy(LoanBuyEvent $event) {
        $logModel = new LoanApplicationLog();
        $logModel->user_id = $event->user->id;
        $logModel->loan_id = $event->loan->id;
        $logModel->org_root = $event->user->org_root;
        $logModel->org_code = $event->user->org_code;

        $logModel->comment = sprintf("%s 抢单成功", $event->user->name);
        $logModel->save();
    }

    /**
     * @param LoanTagEvent $event
     * @return void
     */
    public function handleTag(LoanTagEvent $event) {

        if (empty($event->detached) && empty($event->attached)) return;

        $logModel = new LoanApplicationLog();
        $logModel->user_id = $event->user->id;
        $logModel->loan_id = $event->loan->id;
        $logModel->org_root = $event->user->org_root;
        $logModel->org_code = $event->user->org_code;

        $comment = '';
        if (!empty($event->detached)) {
           $detached = LoanTag::whereIn('id', $event->detached)->select('title')->get()
               ->pluck('title')->toArray();
           $comment .= sprintf("删除标签: %s ", implode(", ", $detached));
        }

        if (!empty($event->attached)) {
            $attached = LoanTag::whereIn('id', $event->attached)->select('title')->get()
                ->pluck('title')->toArray();
            $comment .= sprintf("添加标签: %s ", implode(", ", $attached));
        }


        $logModel->comment = $comment;
        $logModel->save();
    }

    /**
     * Handle the event.
     *
     * @param  object  $events
     * @return array
     */
    public function subscribe($events)
    {
        return [
            AllocateEvent::class => 'handleAllocate',
            UpdateEvent::class => 'handleUpdate',
            LoanBuyEvent::class => 'handleBuy',
            LoanTagEvent::class => 'handleTag',
        ];
    }
}
